﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Hyt.Core.Authentication;
using Hyt.Core.Security;
using Hyt.Passport;
using Hyt.Web.Mvc;
using Hyt.Passport.Models;

namespace Huayuan.Manage.Common
{
    public abstract class BaseViewPage<TModel> : WebViewPage<TModel> where TModel : class
    {

        public HytHtmlHelper<TModel> HytHtml { get; set; }

        public override void Execute()
        {
            
        }
        public override void InitHelpers()
        {
            base.InitHelpers();
            this.HytHtml = new HytHtmlHelper<TModel>(base.ViewContext, this);
        }
        protected override void InitializePage()
        {
            base.InitializePage();
        }

        public Administrator CurrentUser
        {
            get
            {
                using (var ufw = new WebUnitOfWork())
                {
                    var principal = ufw.GetCurrentPrincipal();
                    return principal as Administrator;
                }
            }
        }

        public ViewBagObject VBO
        {
            get
            {
                var o = ViewBag.ViewBagObject;
                if (o == null)
                {
                    ViewBagObject vo = new ViewBagObject();
                    vo.SetUrl(Url);
                    o = vo;
                    ViewBag.ViewBagObject = o;
                }
                return o;
            }
        }

        public bool Cando(string operation)
        {
            using (var ufw = new WebUnitOfWork())
            {
                var principal = ufw.GetCurrentPrincipal();
                if (principal == Principal.Anonymous)
                {
                    return false;
                }
                var authmanager = ufw.Get<IAuthManager>();
                var authresult = authmanager.Check(principal, operation);
                ufw.Commit();
                return authresult.Status;
            }
        }
    }
}